This class is provided as part of Dragonsmith 1.1 but can be used by any application developed with THINK C.
Features
--------
The Preferences class provides simple resource-based preferences management, allowing an application to easily implement a standard preferences file. The class is reasonably failure-tolerant -- with a modicum of defensive programming, an application can function regardless of whether a preferences file was created or any particular preference resource was found.
Implementation
--------------
The various preferences resources are referenced by a single 'PrRo' 128 ("preferences roster") resource that will be copied from the application file to a newly created preferences file. Each preferences resource is designated by the index of its entry in this resource.
An application which uses an object of this class should do something like this in its initialization code, while the application file is still the most recently opened resources file:
#define prefsFileType 'pref' // This is the recommended value (see Preferences.h)
// Here, find the preferences file and store an FSSpec to it in prefsFileSpec
if (allOK)
allOK = preferences->UseFile (&prefsFileSpec);
A particular preferences resource can then be obtained by calling
prefsObject->GetPref(prefIndex);
where prefIndex is the desired preference's index within the 'PrRo' 128 resource.
The resource can then be treated like any other resource, except that you must NOT ReleaseResource or DetachResource it -- call
prefsObject->DetachPrefResource(prefIndex)
and
prefsObject->ReleasePrefResource(prefIndex)
instead, or serious problems may occur later!
If the user double-clicks on a preferences file belonging to your application, you can just call
allOK = preferences->UseFile(&theFileSpec);
and read in the preferences if it succeeds.
See the Dragon.c file in the Dragonsmith 1.1 release for an example of how to use the Preferences class -- especially, how to locate the application's preferences file.